<?php
/**
 * Created by PhpStorm.
 * User: 伟鑫
 * Date: 2019/3/13
 * Time: 13:46
 */

namespace app\index\controller;

use think\Db;
use Yansongda\Pay\Pay;
<<<<<<< HEAD
use app\index\lib\Pay as Webpay;
=======
use app\index\lib\Pay as Jspay;
use Yansongda\Pay\Log;
>>>>>>> 98b4d510f6bc858c89e59222fbe93ccf103e8629
class Topany extends WxBase
{
    protected $config = [
//        'appid' => 'wxb3fxxxxxxxxxxx', // APP APPID
        'app_id' => 'wx75bbccf096140543', // 公众号 APPID
        'miniapp_id' => 'wx13841ad017b28f4c', // 小程序 APPID
        'mch_id' => '1516593971',
        'key' => 'e10adc3949ba59abbe56e057f20f883e',
        'notify_url' => 'https://wukongweixiu.cn/index/Topany/notify',
        //'cert_client' => './cert/apiclient_cert.pem', // optional，退款等情况时用到
        //'cert_key' => './cert/apiclient_key.pem',// optional，退款等情况时用到
        'log' => [ // optional
            'file' => './logs/wechat.log',
            'level' => 'info', // 建议生产环境等级调整为 info，开发环境为 debug
            'type' => 'single', // optional, 可选 daily.
            'max_file' => 30, // optional, 当 type 为 daily 时有效，默认 30 天
        ],
        'http' => [ // optional
            'timeout' => 5.0,
            'connect_timeout' => 5.0,
            // 更多配置项请参考 [Guzzle](https://guzzle-cn.readthedocs.io/zh_CN/latest/request-options.html)
        ],
        //'mode' => 'dev', // optional, dev/hk;当为 `hk` 时，为香港 gateway。
    ];

    public function topay(){
        $order_mun = input('order_mun');
        $paystatus = Db::name('orders')->where('order_mun',$order_mun)->find();
        if ($paystatus['status'] == 4){
            echo '该订单已经支付完成啦....';
            die;
        }
<<<<<<< HEAD
//        $toatle = $this->getTotalPrice($order_mun);
        $orders = Db::name('weixiou_j')->where('orderid',$order_mun)->find();
        $order = [

=======
        $orders = Db::name('weixiou_j')->where('orderid',$order_mun)->find();
        $order = [
>>>>>>> 98b4d510f6bc858c89e59222fbe93ccf103e8629
            'out_trade_no' => $orders['new_oreder_mun'].'_h',
            'total_fee' => $orders['total_price']*100, // **单位：分**
            'body' => '家政服务',
        ];
        $pay = Pay::wechat($this->config)->scan($order);
<<<<<<< HEAD


        $redirectUri = 'https://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'];
//
        $payObj = new Webpay($redirectUri);
        $authInfo = $payObj->getAuthInfo();
        if(!empty($authInfo['openId'])){
            $payInfo = array(
                'body'=>'会员认证',
=======
        $this->assign('scan','55');
        $this->assign('orderid',$order_mun);
        $this->assign('paystatus',$paystatus);
        $this->assign('toatle',$orders['total_price']);
        //jsapi 支付
        $redirectUri = 'https://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'];
        $payObj = new Jspay($redirectUri);
        $authInfo = $payObj->getAuthInfo();
        if(!empty($authInfo['openId'])){
            $payInfo = array(
                'body'=>'家政服务',
>>>>>>> 98b4d510f6bc858c89e59222fbe93ccf103e8629
                'orderSn'=>$orders['new_oreder_mun'],
                'fee'=>$orders['total_price'],
                'openId'=>$authInfo['openId'],
            );
            $payRes = $payObj->toPay($payInfo);
            $this->assign('payRes',$payRes);
            $this->assign('payInfo',$payInfo);
<<<<<<< HEAD

        }
        $this->assign('scan',$pay->code_url);
        $this->assign('toatle',$orders['total_price']);
        $this->assign('order',$paystatus);
=======
        }
>>>>>>> 98b4d510f6bc858c89e59222fbe93ccf103e8629
        return $this->fetch();
    }

    public function notify(){
        $pay = Pay::wechat($this->config);
        $data = $pay->verify();
        $notify = json_decode(json_encode($data->all()),true);
        if (isset($notify['return_code']) && $notify['return_code'] == 'SUCCESS' && isset($notify['out_trade_no']) && !empty($notify['out_trade_no'])){
            $order_mun = explode('_',$data->out_trade_no);
<<<<<<< HEAD
            $oder = \db('')->where('new_oreder_mun',$order_mun[0])->find();
            if ($oder){
                db('weixiou')->where('orderid',$oder['orderid'])->update(['type'=>2]);
                db('orders')->where('order_mun',$oder['orderid'])->update(['status' => 4]);
            }


            //支付尾款的师傅佣金提成和推荐人提成
            qianbao($oder['total_price'],$order_mun[0]);
=======
            if (isset($order_mun[0])){
                $order = Db::name('weixiou_j')->where('new_oreder_mun',$data->out_trade_no)->find();
                if (!empty($order)){
                    db('weixiou')->where('orderid',$order['orderid'])->update(['type'=>2]);
                    db('orders')->where('order_mun',$order['orderid'])->update(['status' => 4]);
//                $toatle = $this->getTotalPrice($order['orderid']);
                    //支付尾款的师傅佣金提成和推荐人提成
//                qianbao($toatle,$order['orderid']);
                }
            }


>>>>>>> 98b4d510f6bc858c89e59222fbe93ccf103e8629
            return $pay->success()->send();
        }
    }


    public function getTotalPrice($order_mun){
        $order = Db::name('weixiou_j')->where('orderid',$order_mun)->find();

        return $order['total_price'];
    }

    //用户退款
    public function tuikuan(){
        try{
            $order_mun = input('order_mun');
            $toatle = $this->getTotalPrice($order_mun);
            $order= [
                'type' => 'miniapp',
                'out_trade_no' => '1514192025',
                'out_refund_no' => time(),
                'total_fee' => $toatle,
                'refund_fee' => $toatle,
                'refund_desc' => '测试退款haha',
            ];
            $result =  Pay::wechat($this->config)->refund($order);
            $f = fopen('tui.txt','a+');
            fwrite($f,'111>>');
            fwrite($f,json_encode($result));
            fclose($f);
        }catch (\Exception $e){
            $f = fopen('tui.txt','a+');
            fwrite($f,'111>>');
            fwrite($f,$e->getMessage());
            fclose($f);
        }

    }

}